05 - 盒子模型

选择器 3

结构伪类选择器 Structural Pseudo-Class Selectors

选择器 描述
:first-child 选择父元素的第一个子元素。
:last-child 选择父元素的最后一个子元素。
:nth-child(n) 选择父元素的第 n 个子元素,可以使用具体的数字、公式或关键字(如 even、odd)。
偶数标签 2n,基数标签 2n±1
5 的倍数的标签 5n,第 5 个以后的标签 n+5,第 5 个以前的标签 -n+5
:nth-child(even) 选择父元素下所有偶数位置的子元素。
:nth-child(odd) 选择父元素下所有奇数位置的子元素。
-- --
:nth-last-child(n) 选择父元素的倒数第 n 个子元素,同样可以使用数字、公式或关键字。
:first-of-type 选择父元素下同类型的第一个子元素。
:last-of-type 选择父元素下同类型的最后一个子元素。
:nth-of-type(n) 选择父元素下同类型的第 n 个子元素。
:nth-last-of-type(n) 选择父元素下同类型的倒数第 n 个子元素。

<!DOCTYPE html>
<html>
  <head>
    <style>
      /* 选择第一个子元素 */
      .list li:first-child {
        font-weight: bold;
      }

      /* 选择最后一个子元素 */
      .list li:last-child {
        font-style: italic;
      }

      /* 选择第三个子元素 */
      .list li:nth-child(3) {
        color: red;
      }

      /* 选择偶数位置的子元素 */
      .list li:nth-child(even) {
        background-color: lightgray;
      }

      /* 选择奇数位置的子元素 */
      .list li:nth-child(odd) {
        background-color: lightblue;
      }
    </style>
  </head>
  <body>
    <ul class="list">
      <li>苹果</li>
      <li>香蕉</li>
      <li>橙子</li>
      <li>草莓</li>
      <li>蓝莓</li>
    </ul>
  </body>
</html>
:nth-child(n):nth-of-type(n) 区别

  • :nth-child(n) 选择器与父元素下所有子元素的位置有关。例如:ul li:nth-child(2) 选中 ul 元素下的第二个子元素,无论是什么类型的元素。
  • :nth-of-type(n) 选择器与同类型的子元素的位置有关。例如:ul li:nth-of-type(2) 选中 ul 元素下类型为 li 元素的第二个子元素。

<ul> 
  <li> 列表项 1</li> 
  <li> 列表项 2</li> 
  <li> 列表项 3</li> 
  <li> 列表项 4</li> 
  <li> 列表项 5</li> 
  <div> 其他元素 1</div> 
  <li> 列表项 6</li> 
  <div> 其他元素 2</div> 
</ul> 
/* 使用:nth-child(n) 选择器 */
ul li:nth-child(3) {
  color: red;
}

/* 使用:nth-of-type(n) 选择器 */
ul li:nth-of-type(3) {
  color: blue;
}
  • 使用 :nth-child(3) 选择器,会选中第 3 个子元素,即列表项 3。所以列表项 3 的颜色将被设置为红色。
  • 使用 :nth-of-type(3) 选择器,会选中第 3 个类型为 li 的子元素,即列表项 2。所以列表项 2 的颜色将被设置为蓝色。

伪元素选择器 Pseudo-Element Selectors

伪元素选择器 描述
::after 在被选中元素的内容的最后面插入一个虚拟的元素
::before 在被选中元素的内容的最前面插入一个虚拟的元素
::first-letter 将被选中元素的第一个字母或中文字符设置特殊样式
::first-line 将被选中元素的第一行设置特殊样式
::selection 选中文本时设置选中部分的样式

<!DOCTYPE html>
<html>
  <head>
    <style>
      /* 添加引号到段落之前 */
      .quote p::before {
        content: "“";
        font-size: 1.5em;
        color: gray;
      }

      /* 添加引号到段落之后 */
      .quote p::after {
        content: "”";
        font-size: 1.5em;
        color: gray;
      }
    </style>
  </head>
  <body>
    <div class="quote">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </body>
</html>

PxCook 用法

盒子模型

盒子模型部分 CSS 属性名 描述
内容区 width 内容区即元素的实际内容,它的宽度由该属性决定。
height 内容区即元素的实际内容,它的高度由该属性决定。
内边距 padding 内边距指的是元素内容与边框之间的空间,用于控制元素内容与边框之间的距离。
该属性可以设置上、右、下、左四个方向的内边距值,也可以分别设置每个方向的内边距值。
边框 border 边框是围绕元素内容和内边距的线条,用于包围元素。
该属性可以设置边框的样式、宽度和颜色。不同方向的边框样式可以分别设置,也可以设置统一的边框样式。
外边距 margin 外边距指的是元素与相邻元素之间的空间,用于控制元素与其他元素之间的距离。
该属性可以设置上、右、下、左四个方向的外边距值,也可以分别设置每个方向的外边距值。
总宽度和高度 (标准盒子模型) 元素的总宽度和高度由内容区、内边距、边框和外边距的尺寸之和决定,不具体对应单一的 CSS 属性,而是通过各部分属性的设置共同决定。
具体计算公式为: 宽度 = 左外边距 + 左边框 + 左内边距 + 内容区宽度 + 右内边距 + 右边框 + 右外边距 同样的规则适用于高度的计算。

组成

CSS 盒子模型由以下四个主要部分组成:

div {
  margin: 50px;
  border: 5px solid brown;
  padding: 20px;
  width: 200px;
  height: 200px;
  background-color: pink;
}

边框 border

属性 描述 取值
border 设置元素的边框 宽度 样式 颜色
border-width 设置元素边框的宽度 数值或 thinmediumthick
border-style 设置元素边框的样式 nonehiddendotteddashedsoliddoublegrooveridgeinsetoutset
border-color 设置元素边框的颜色 颜色值、transparent
border-top 设置元素顶部边框 宽度 样式 颜色
border-right 设置元素右侧边框 宽度 样式 颜色
border-bottom 设置元素底部边框 宽度 样式 颜色
border-left 设置元素左侧边框 宽度 样式 颜色

<!DOCTYPE html>
<html>
  <head>
    <style>
      .box {
        width: 200px;
        height: 100px;
        border: 2px solid black; /* 边框样式 */
        padding: 20px;
      }
    </style>
  </head>
  <body>
    <div class="box">This is a box with a border.</div>
  </body>
</html>

内边距 padding

属性 描述
padding 设置元素的内边距
padding-top 设置元素顶部内边距
padding-right 设置元素右侧内边距
padding-bottom 设置元素底部内边距
padding-left 设置元素左侧内边距

padding 多值写法

描述 示例
单个值 所有 4 个方向的内边距均相等 padding: 10px;
两个值 上下内边距相等,左右内边距相等 padding: 10px 20px;
三个值 上内边距,左右内边距相等,下内边距 padding: 10px 20px 30px;
四个值 上、右、下、左内边距各自独立设置 padding: 10px 20px 30px 40px;
<!DOCTYPE html>
<html>
  <head>
    <style>
      .box {
        width: 200px;
        height: 100px;
        border: 2px solid black;
        padding: 10px 20px 30px 40px; /* 内边距样式:上 右 下 左 */
      }
    </style>
  </head>
  <body>
    <div class="box">This is a box with different paddings.</div>
  </body>
</html>

外边距 margin

margin 多值写法

描述 示例
单个值 所有 4 个方向的外边距均相等 margin: 10px;
两个值 上下外边距相等,左右外边距相等 margin: 10px 20px;
三个值 上外边距,左右外边距相等,下外边距 margin: 10px 20px 30px;
四个值 上、右、下、左外边距各自独立设置 margin: 10px 20px 30px 40px;

尺寸计算

版心居中

.box {
  margin: 0 auto;
  width: 1000px;
  height: 200px;
  background-color: pink;
}

清除默认样式

默认样式

默认样式的意义

默认样式带来的问题

CSS Reset

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>default style</title>
  </head>

  <body>
    <div>DIV 元素</div>
    <ul>
      <li>LI 元素</li>
      <li>LI 元素</li>
    </ul>
  </body>
</html>

使用通用选择器

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>default style</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
    </style>
  </head>

  <body>
    <div>DIV 元素</div>
    <ul>
      <li>LI 元素</li>
      <li>LI 元素</li>
    </ul>
  </body>
</html>

样式归零

/* http://meyerweb.com/eric/tools/css/reset/
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}

样式调整

Normalize.css makes browsers render all elements more consistently and in line with modern standards. It precisely targets only the styles that need normalizing.

内容溢出 overflow

属性值 描述
visible(默认值) 内容溢出时,会超出元素的边界,不会被修剪,会覆盖到其他元素上方。
hidden 内容溢出时,会被修剪,超出元素的部分将被隐藏,不会显示在页面上。
scroll 内容溢出时,会显示滚动条,用户可以通过滚动条来查看超出元素的部分。
auto 内容溢出时,会根据情况显示滚动条。如果内容未溢出,则不显示滚动条;如果内容溢出,则显示滚动条。
功能 CSS 样式
防止内容溢出破坏布局 overflow: hidden;
创建可滚动的容器 overflow: scroll;overflow: auto;
隐藏多行文本的溢出 white-space: nowrap;text-overflow: ellipsis;

<!DOCTYPE html>
<html>
  <head>
    <style>
      .container {
        width: 200px;
        height: 100px;
        border: 2px solid black;
        overflow: auto; /* 自动显示滚动条 */
      }

      .content {
        padding: 10px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="content">
        This is some overflowing content. This is some overflowing content. This is some overflowing content.
      </div>
    </div>
  </body>
</html>

外边距合并与塌陷 Margin Collapsing

MDN 文档

外边距合并

我们给 child1 设置了 margin-bottom:20px,给 child2 设置了 margin-top: 50px,但最终的展现效果确实 50px


<!DOCTYPE html>
<html>
  <head>
    <style>
        .box1 {
        width: 100px;
        height: 100px;
        margin-bottom: 20px;
          background-color: pink;
      }

      .box2 {
        width: 100px;
        height: 100px;
        margin-top: 30px;
          background-color: orange;
      }
    </style>
  </head>
  <body>
    <div class="box1">Box 1</div>
    <div class="box2">Box 2</div>
  </body>
</html>

外边距塌陷

我们给 child 添加了 margin-top,却导致 body 整体下移。

行内元素 – 内外边距问题

span {
  /* margin 和 padding 属性,无法改变垂直位置 */
  margin: 50px;
  padding: 20px;
  /* 行高可以改变垂直位置 */
  line-height: 100px;
}

圆角 border-radius

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>border-radius 实现圆角效果</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        font-family: "Montserrat", sans-serif;
      }

      html,
      body {
        width: 100%;
        height: 100%;
        padding: 30px;
        background-color: #000;
      }

      p {
        font-size: 20px;
        color: #fff;
      }

      hr {
        margin-bottom: 40px;
      }

      .sample {
        display: flex;
        align-items: center;
        margin-bottom: 20px;
      }

      .box-square {
        width: 100px;
        height: 100px;
        margin-right: 60px;
      }

      .box-rectangle {
        width: 100px;
        height: 50px;
        margin-right: 60px;
      }

      .box1 {
        border-radius: 10px;
        background: linear-gradient(to left, #fc466b, #3f5efb);
      }

      .box2 {
        border-radius: 60px 25px;
        background: linear-gradient(to left, #fc466b, #3f5efb);
      }

      .box3 {
        border-radius: 60px 25px 10px;
        background: linear-gradient(to left, #fc466b, #3f5efb);
      }

      .box4 {
        border-radius: 60px 25px 10px 5px;
        background: linear-gradient(to left, #fc466b, #3f5efb);
      }

      .box5 {
        border-radius: 50px;
        background: linear-gradient(to left, #36d1dc, #5b86e5);
      }

      .box6 {
        border-radius: 25px;
        background: linear-gradient(to left, #36d1dc, #5b86e5);
      }

      .box7 {
        border-radius: 50px/10px;
        background: linear-gradient(to left, #c02425, #f0cb35);
      }

      .box8 {
        border-radius: 50px 15px / 10px 25px;
        background: linear-gradient(to left, #c02425, #f0cb35);
      }

      .box9 {
        border-radius: 50px 50px 30px / 10px 25px 50px;
        background: linear-gradient(to left, #c02425, #f0cb35);
      }

      .box10 {
        border-radius: 50px 50px 30px 5px / 10px 25px 50px 30px;
        background: linear-gradient(to left, #c02425, #f0cb35);
      }

      .box11 {
        border-radius: 50px 50px 30px 5px / 50px;
        background: linear-gradient(to left, #c02425, #f0cb35);
      }

      .box12 {
        border-radius: 50px 10px / 30px;
        background: linear-gradient(to left, #c02425, #f0cb35);
      }
    </style>
  </head>

  <body>
    <div class="sample">
      <div class="box-square box1"></div>
      <p>border-radius: 10px;</p>
    </div>
    <div class="sample">
      <div class="box-square box2"></div>
      <p>border-radius:60px 25px;</p>
    </div>
    <div class="sample">
      <div class="box-square box3"></div>
      <p>border-radius:60px 25px 10px;</p>
    </div>
    <div class="sample">
      <div class="box-square box4"></div>
      <p>border-radius:60px 25px 10px 5px;</p>
    </div>
    <hr />
    <div class="sample">
      <div class="box-square box5"></div>
      <p>border-radius: 50px;</p>
    </div>
    <div class="sample">
      <div class="box-rectangle box6"></div>
      <p>border-radius: 25px;</p>
    </div>
    <hr />
    <div class="sample">
      <div class="box-square box7"></div>
      <p>border-radius: 50px/10px;</p>
    </div>
    <div class="sample">
      <div class="box-square box8"></div>
      <p>border-radius: 50px 15px / 10px 25px;;</p>
    </div>
    <div class="sample">
      <div class="box-square box9"></div>
      <p>border-radius: 50px 50px 30px / 10px 25px 50px ;</p>
    </div>
    <div class="sample">
      <div class="box-square box10"></div>
      <p>border-radius: 50px 50px 30px 5px / 10px 25px 50px 30px;</p>
    </div>
    <div class="sample">
      <div class="box-square box11"></div>
      <p>border-radius: 50px 50px 30px 5px / 50px;</p>
    </div>
    <div class="sample">
      <div class="box-square box12"></div>
      <p>border-radius: 50px 10px / 30px;</p>
    </div>
  </body>
</html>

复合写法 - 不带 / 的值

值的个数 简写 等价于
1 border-radius: 10px; border-top-left-radius: 10px;
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
2 border-radius: 10px 20px; border-top-left-radius: 10px;
border-top-right-radius: 20px;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 20px;
3 border-radius: 10px 20px 30px; border-top-left-radius: 10px;
border-top-right-radius: 20px;
border-bottom-right-radius: 30px;
border-bottom-left-radius: 20px;
4 border-radius: 10px 20px 30px 40px; border-top-left-radius: 10px;
border-top-right-radius: 20px;
border-bottom-right-radius: 30px;
border-bottom-left-radius: 40px;

复合写法 - 带 / 的值

值的个数 简写 等价于
1 border-radius: 10px/20px; border-top-left-radius: 10px 20px;
border-top-right-radius: 10px 20px;
border-bottom-right-radius: 10px 20px;
border-bottom-left-radius: 10px 20px;
2 border-radius: 10px 20px/30px 40px; border-top-left-radius: 10px 30px;
border-top-right-radius: 20px 40px;
border-bottom-right-radius: 10px 30px;
border-bottom-left-radius: 20px 40px;
3 border-radius: 10px 20px 30px/20px 30px 40px; border-top-left-radius: 10px 20px;
border-top-right-radius: 20px 30px;
border-bottom-right-radius: 30px 40px;
border-bottom-left-radius: 20px 30px;
4 border-radius: 10px 20px 30px 40px/20px 30px 40px 50px; border-top-left-radius: 10px 20px;
border-top-right-radius: 20px 30px;
border-bottom-right-radius: 30px 40px;
border-bottom-left-radius: 40px 50px;

盒子阴影 box-shadow

/* x 偏移量 | y 偏移量 | 阴影颜色 */
box-shadow: 60px -16px teal;

/* x 偏移量 | y 偏移量 | 阴影模糊半径 | 阴影颜色 */
box-shadow: 10px 5px 5px black;

/* x 偏移量 | y 偏移量 | 阴影模糊半径 | 阴影扩散半径 | 阴影颜色 */
box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.2);

/* 插页 (阴影向内) | x 偏移量 | y 偏移量 | 阴影颜色 */
box-shadow: inset 5em 1em gold;

/* 任意数量的阴影,以逗号分隔 */
box-shadow: 3px 3px red, -1em 0 0.4em olive;

/* 全局关键字 */
box-shadow: inherit;
box-shadow: initial;
box-shadow: unset;

取值

列表中的每个阴影通过 color 组件(作为颜色),以及 x,y,blur,(合适的时候)加上 spread 组件(作为长度)进行合成。对于每个阴影,如果两个输入的阴影都是 inset 或者都不是 inset,那么要添加的阴影必须考虑已存在的阴影。如果任何一对输入阴影中,一个是 inset,另一个不是 inset,那么整个阴影列表就是不可合成的。如果阴影列表有不同的长度,那么较短的列表会在尾部补上这类阴影:颜色透明,所有长度为 0,inset 还是非 inset 同较长的列表。

语法

box-shadow =
  none       |
  <shadow>#

<shadow> =
  <color>?                                   &&
  [ <length>{2} <length [0,∞]>? <length>? ]  &&
  inset?


综合案例 5

案例 - 产品卡片

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>产品卡片</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      background-color: #F5F5F5;
    }

    .product {
      margin: 50px auto;
      padding-top: 40px;

      width: 300px;
      height: 250px;
      background-color: #fff;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0, 0, 0, .1);
      text-align: center;
    }

    .product__img {
      width: 80px;
      height: 80px;
      margin: 0 auto;
    }

    .product__title {
      margin-top: 20px;
      font-size: 18px;
      font-weight: bold;
      color: #333;
    }

    .product__desc {
      margin-top: 10px;
      font-size: 14px;
      color: #999;
    }
  </style>
</head>

<body>
  <div class="product">
    <div class="product__img">
      <svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
        <circle cx="40" cy="40" r="40" fill="#FE2C55" />
        <path
          d="M59.0472 51.1156L58.0751 50.7528C57.589 50.5109 56.9409 50.5109 56.4549 50.7528L52.2423 52.325L39.9287 56.9206L23.5645 50.7528C23.0784 50.5109 22.4303 50.5109 21.9443 50.7528L20.9721 51.1156C19.676 51.5994 19.676 52.9297 20.9721 53.4134L39.1186 60.1859C39.6046 60.4278 40.2527 60.4278 40.7388 60.1859L58.8852 53.4134C60.3434 52.9297 60.3434 51.5994 59.0472 51.1156Z"
          fill="white" />
        <path
          d="M20.9756 28.3887L39.187 34.5294C39.6748 34.7487 40.3252 34.7487 40.813 34.5294L59.0244 28.3887C60.3252 27.95 60.3252 26.7438 59.0244 26.3052L40.813 20.1645C40.3252 19.9452 39.6748 19.9452 39.187 20.1645L20.9756 26.3052C19.6748 26.7438 19.6748 27.95 20.9756 28.3887Z"
          fill="white" />
        <path
          d="M36.3265 50.2424V40.7464C36.3265 39.788 37.0099 38.9648 37.9684 38.9594C40.3521 38.9461 44.4898 39.6983 44.4898 45.0817C44.4898 51.1627 39.2101 51.3345 37.1427 51.1686C36.665 51.1303 36.3265 50.7216 36.3265 50.2424Z"
          stroke="white" stroke-width="3" stroke-linecap="round" />
        <path
          d="M31 37L26.748 35.1398C24.9803 34.3664 23 35.6615 23 37.591V37.591C23 38.7507 23.6865 39.8003 24.7489 40.2651L29.4 42.3C30.372 42.7252 31 43.6855 31 44.7464V46.442C31 47.8843 29.5197 48.8524 28.1984 48.2743L23 46"
          stroke="white" stroke-width="3" stroke-linecap="round" />
        <path fill-rule="evenodd" clip-rule="evenodd"
          d="M58.2076 34.3898C58.699 33.7228 58.5567 32.7838 57.8898 32.2924C57.2229 31.801 56.2838 31.9432 55.7924 32.6102L51.5 38.4356V36C51.5 35.1715 50.8284 34.5 50 34.5C49.1716 34.5 48.5 35.1715 48.5 36V43V49C48.5 49.8284 49.1716 50.5 50 50.5C50.8284 50.5 51.5 49.8284 51.5 49V43.4929L52.3464 42.3442L55.7612 47.3459C56.2283 48.03 57.1616 48.206 57.8458 47.7389C58.53 47.2718 58.7059 46.3385 58.2388 45.6543L54.4837 40.1542C54.4004 40.0322 54.3024 39.9264 54.1935 39.8375L58.2076 34.3898Z"
          fill="white" />
      </svg>
    </div>
    <div class="product__info">
      <div class="product__title">抖音直播 SDK</div>
      <div class="product__desc">包含抖音直播看播功能</div>
    </div>
  </div>
</body>

</html>

案例 - 新浪新闻

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>新浪新闻</title>
    <style>
      /* reset */
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      a {
        text-decoration: none;
        color: #333;
      }

      li {
        list-style: none;
      }

      /* news */
      .news {
        width: 500px;
        margin: 50px auto;
      }

      .news__head {
        height: 36px;
        line-height: 36px;
        background-color: #eee;
        border: 1px solid #f1f1f1;
      }

      .news__head a {
        display: inline-block;
        margin-top: -1px;
        padding: 0 10px;
        border-top: 3px solid orange;
        font-size: 14px;
        background-color: #fff;
        color: #333;
      }

      .news__head a:hover {
        background-color: #f5f5f5;
      }

      .news__list {
        padding: 10px;
      }

      .news__list ul li {
        padding-left: 20px;
        background: url("https://s2.loli.net/2023/08/26/NY83GleBXJwZnRs.png") 0 center no-repeat;

        height: 36px;
        line-height: 36px;
        border-bottom: 1px solid #f1f1f1;
      }

      .news__list ul li a {
        padding-left: 20px;
        background: url("https://s2.loli.net/2023/08/26/CAuxEhp52qi7e6g.gif") 0 center no-repeat;
      }

      .news__list ul li a:hover {
        color: #ff8400;
      }
    </style>
  </head>

  <body>
    <div class="news">
      <div class="news__head">
        <a href="#">新闻</a>
      </div>
      <div class="news__list">
        <ul>
          <li><a href="#">点赞“新农人”温暖的伸手</a></li>
          <li><a href="#">在希望的田野上...</a></li>
          <li><a href="#">“中国天眼”又有新发现 已在《自然》杂志发表</a></li>
          <li><a href="#">急!这个领域,缺人!月薪 4 万元还不好招!啥情况?</a></li>
          <li><a href="#">G9“带货”背后:亏损面持续扩大,竞争环境激烈</a></li>
          <li><a href="#">多地力推二手房“带押过户”,有什么好处?</a></li>
        </ul>
      </div>
    </div>
  </body>
</html>